<?php
/**
 * Created by PhpStorm.
 * User: Talent Gao
 * Date: 14-8-19
 * Time: 下午2:26
 */

class IndexModel extends Db_Base{
    const F1_Cache_KEY = 'f1_index';
    const F2_Cache_KEY = 'f2_index';
    const F3_Cache_KEY = 'f3_index';

    public function __construct() {
        parent::__construct();
    }

    /**
     * HomeData
     * @return array
     */
    public function getHomeData(){

        $f1_index = $this->getAdvInfo('f1_index');
        $f2_index = $this->getAdvInfo('f2_index');
        $f3_index = $this->getAdvInfo('f3_index');

        $data = array(
            'f1_index'    => $f1_index,
            'f2_index'    => $f2_index,
            'f3_index'    => $f3_index
        );

        return $data;
    }

    /**
     * Through the floor for specific products
     * @param $module
     * @return array|mixed
     */
    public function getAdvInfo($module)
    {
        $key    = $this->_getContentCacheKey($module);

        $data = $this->_cache_handler->load($key);

        if (!empty($data) && !empty($data))
        {
            return $data;
        }

        //F1数据
        if($module == self::F1_Cache_KEY){
            $data = $this->F1_Index();
        }

        //F2数据
        if($module == self::F2_Cache_KEY){
            $data = $this->F2_Index();
        }

        //F3数据
        if($module == self::F3_Cache_KEY){
            $data = $this->F3_Index();
        }

        $this->_cache_handler->put($key, $data, 300);//缓存5分钟

        return $data;
    }

    /**
     * getCateInfo();获取分类信息
     * @param $module
     * @param $offset1
     * @param $offset2
     * @return array|mixed
     */

    public function getCateInfo($module,$offset1=0,$offset2=0){
        // Cache Key
        $key    = $this->_getContentCacheKey($module);
        //get  Cache
        $data = $this->_cache_handler->load($key);
        if (!empty($data))
        {
            return $data;
        }
        $data = array();
        $category = new CategoryModel();
        $data = $category->getCates();
        if(!empty($data)){
            foreach($data as $key=>$var){
                $data[$key]['num'] = $category->getProductNum($var['cate_id']);
                $data[$key]['list'] = $category->getProductInfo($var['cate_id'],$offset1,$offset2);
            }
        }
        $this->_cache_handler->put($key, $data, 300);//缓存5分钟
        return $data;
    }

    /**
     * getIndexMark 获取今日的原油指数
     * @param $module
     * @param $module
     * @return array|mixed|void
     */
    public function getIndexMark($module){
        // Cache Key
        $key    = $this->_getContentCacheKey($module);
        //get  Cache
        $data = $this->_cache_handler->load($key);
        if (!empty($data))
        {
            return $data;
        }
        $common = new CommonModel();
        $data = array();
        $data = $common->getIndex();
        $this->_cache_handler->put($key, $data, 300);//缓存5分钟
        return $data;
    }

    /**
     * @return array
     */
    private function F1_Index(){
        //修改条件在这边添加
        //todo 未添加状态
        $where="iq.product_id>0 AND iq.status= '2' AND iq.area_id <>'' AND iq.currency_id <> ''";
        $order = "iq.update_time desc";
        $limit = "limit 0,10";
        $findex = new FindexModel("F1_A_Index",$where,$order,$limit);
        $data = $findex->getShowIndex();
        if(!empty($data)){
            foreach($data as $key=>$val){
                $data[$key]['update_time'] =Tools::time2Units(time () - strtotime ( $val ['update_time'] )) . "前";
            }
        }
        return $data;
    }

    /**
     * @return array
     */
    private function F2_Index(){
        //todo 未添加状态，待报价，报价中其他的不显示
        $where="iq.product_id>0 AND iq.status in('2','3')";
        $order = "iq.update_time desc";
        $limit = "limit 0,10";
        $findex = new FindexModel("F2_B_Index",$where,$order,$limit);
        $data = $findex->getShowIndex();
        if(!empty($data)){
            foreach($data as $key=>$val){
                $data[$key]['update_time'] =Tools::time2Units(time () - strtotime ( $val ['update_time'] )) . "前";
            }
        }
        return $data;
    }

    /**
     * @return array
     */
    private function F3_Index(){
        global $_suppliyStateCfg;
        //todo 未添加状态
        $where="1=1 and iq.status in ({$_suppliyStateCfg['up']}) and `iq`.`business_id` <> '' ";
        $order = "max(`iq`.`update_time`) desc";
        $limit = "limit 0,10";
        $findex = new FindexModel("F3_C_Index",$where,$order,$limit);
        $data = $findex->getShowIndex();
        if(!empty($data)){
            foreach($data as $key=>$val){
                $data[$key]['update_time'] =Tools::time2Units(time () - strtotime ( $val ['update_time'] )) . "前";
            }
        }
        return $data;
    }

    /**
     * @param $where
     * @param $limit
     * @return array|mixed
     */
    public function getTradeWhereType($where,$limit){
        $key    = $this->_getContentCacheKey("Trade_F1");
        //get  Cache
        $data = $this->_cache_handler->load($key);
        if (!empty($data))
        {
            return $data;
        }
        $trade = new OrderModel();

        $data = $trade->getOrderTrade($where,$limit);

        $this->_cache_handler->put($key, $data, 300);//缓存5分钟
        return $data;
    }

    /**
     * @param $where
     * @param $limit
     * @return array|mixed
     */
    public function getInquireWhereStatus($where,$limit){
        $key    = $this->_getContentCacheKey("Inquire_F2");
        //get  Cache
        $data = $this->_cache_handler->load($key);
        if (!empty($data))
        {
            return $data;
        }
        $trade = new InquireModel();

        $data = $trade->getNewInquire($where,$limit);

        $this->_cache_handler->put($key, $data, 300);//缓存5分钟
        return $data;
    }

    /**
     * @param $where
     * @param $limit
     * @return array|mixed
     */
    public function getSupplyWhereStatus($where,$limit){
        $key    = $this->_getContentCacheKey("Supply_F3");
        //get  Cache
        $data = $this->_cache_handler->load($key);
        if (!empty($data))
        {
            return $data;
        }
        $trade = new SupplyModel();

        $data = $trade->getSupplyByWhere($where,$limit);

        $this->_cache_handler->put($key, $data, 300);//缓存5分钟
        return $data;
    }

    /**
     * @param $where
     * @param $limit
     * @return array|mixed
     */
    public function getRankingWhereVisit($where,$order,$limit){
        $key    = $this->_getContentCacheKey("Ranking_F3");
        //get  Cache
        $data = $this->_cache_handler->load($key);
        if (!empty($data))
        {
            return $data;
        }
        $trade = new SupplyModel();

        $data = $trade->getRankingByWhere($where,$order,$limit);
        //

        $this->_cache_handler->put($key, $data, 300);//缓存5分钟
        return $data;
    }

    /**
     * @return array|mixed
     */
    public function getNewsInfo($where,$limit){
        $key    = $this->_getContentCacheKey("NEWS_FOOT");
        //get  Cache
        $data = $this->_cache_handler->load($key);
        if (!empty($data))
        {
            return $data;
        }
        $news = new NewsModel();

        $data = $news->getNewsByWhere($where,$limit);
        //查询分类下的子信息
        if(!empty($data)){
            foreach($data as $key=>$var){
                $data[$key]['list'] = $news->getNewsChildById("cate_id = '{$var['id']}'");
            }
        }

        $this->_cache_handler->put($key, $data, 300);//缓存5分钟
        return $data;
    }

    /**
     * @param $where
     * @return array|mixed
     */
    public function getCarousel($where){
//        $key    = $this->_getContentCacheKey("CAROUSEL");
//        //get  Cache
//        $data = $this->_cache_handler->load($key);
//        if (!empty($data))
//        {
//            return $data;
//        }

        $com = new CommonModel();

        $data = $com->getCarouselByWhere($where);

//        $this->_cache_handler->put($key, $data, 300);//缓存5分钟
        return $data;
    }


    /**
     * get Cache Key
     * @param $module
     * @return string
     */
    private function _getContentCacheKey($module) {
        return "adv_cache_B_{$module}_content";
    }
}